package cqrtplm.vo;

import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class OnlineAnnotationVO {
    private String OID;
    private String description;
    private String fileID;
    private String fileName;
    private String flag;

    private String creator;
    private String objOid;
    private String objOType;
    private String base64Img;

    private String imageUrl = "";

    private AttachmentVO image = new AttachmentVO();
    private List<AttachmentVO> attachments = new ArrayList<>();

    public static List<OnlineAnnotationVO> buildVO(List<OnlineAnnotationVO> onlineAnnotationVOS){
        Map<String , OnlineAnnotationVO> map = new HashMap<>();
        for (OnlineAnnotationVO onlineAnnotationVO : onlineAnnotationVOS) {
            if(map.containsKey(onlineAnnotationVO.getOID())){
                OnlineAnnotationVO onlineAnnotation = map.get(onlineAnnotationVO.getOID());
                if("img".equals(onlineAnnotationVO.getFlag())){
                    onlineAnnotation.setImageUrl(onlineAnnotationVO.getBase64Img());
                }else {
                    onlineAnnotation.getAttachments().add(new AttachmentVO(onlineAnnotationVO.getFileID(), onlineAnnotationVO.getFileName()));
                }
            }else {
                OnlineAnnotationVO onlineAnnotation = new OnlineAnnotationVO();
                onlineAnnotation.setOID(onlineAnnotationVO.getOID());
                onlineAnnotation.setDescription(onlineAnnotationVO.getDescription());
                onlineAnnotation.setCreator(onlineAnnotationVO.getCreator());
                if("img".equals(onlineAnnotationVO.getFlag())){
                    onlineAnnotation.setImageUrl(onlineAnnotationVO.getBase64Img());
                }else {
                    onlineAnnotation.getAttachments().add(new AttachmentVO(onlineAnnotationVO.getFileID(), onlineAnnotationVO.getFileName()));
                }
                map.put(onlineAnnotationVO.getOID(), onlineAnnotation);
            }
        }
        return new ArrayList<>(map.values());
    }
}
